package org.infodavid.common.model.impl;

import org.infodavid.common.model.IBasicEvent;
import org.joda.time.LocalDateTime;

/**
 * The Class EventImpl.
 */
public class BasicEventImpl extends AbstractNamedCommented<Long> implements IBasicEvent {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -4382933813517861251L;

	/** The end time. */
	private LocalDateTime endTime;

	/** The start time. */
	private LocalDateTime startTime;

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected BasicEventImpl() throws Exception {
		super();
	}

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected BasicEventImpl(final Long keyArg) throws Exception {
		this();

		setKey(keyArg);
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.bs.model.IEvent#getEndTime()
	 */
	@Override
	public LocalDateTime getEndTime() {
		return endTime;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.bs.model.IEvent#getStartTime()
	 */
	@Override
	public LocalDateTime getStartTime() {
		return startTime;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		endTime = null;
		startTime = null;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.listitems.IBasicEventListItem#setEndTime(org.joda.time.LocalDateTime
	 * )
	 */
	@Override
	public void setEndTime(final LocalDateTime value) {
		if (!equals(endTime, value)) {
			final Object old = endTime;
			endTime = value;
			markModified(END_TIME, old, endTime);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.listitems.IBasicEventListItem#setStartTime(org.joda.time.LocalDateTime
	 * )
	 */
	@Override
	public void setStartTime(final LocalDateTime value) {
		if (!equals(startTime, value)) {
			final Object old = startTime;
			startTime = value;
			markModified(START_TIME, old, startTime);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[startTime=");
		builder.append(startTime);
		builder.append(", endTime=");
		builder.append(endTime);
		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getEndTime() == null ? 0 : getEndTime().hashCode());

		return prime * result + (getStartTime() == null ? 0 : getStartTime().hashCode());
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IBasicEvent)) {
			return false;
		}

		final IBasicEvent other = (IBasicEvent)obj;

		if (getEndTime() == null) {
			if (other.getEndTime() != null) {
				return false;
			}
		}
		else if (!getEndTime().equals(other.getEndTime())) {
			return false;
		}

		if (getStartTime() == null) {
			if (other.getStartTime() != null) {
				return false;
			}
		}
		else if (!getStartTime().equals(other.getStartTime())) {
			return false;
		}

		return true;
	}
}
